home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / ab_tree.h next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  6.3 KB  |  206 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  ab_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_AB_TREE_H
  16. #define LEDA_AB_TREE_H
  17.  
  18. //------------------------------------------------------------------------------
  19. //
  20. // (a,b)-trees 
  21. //
  22. // Evelyn Haak, Juergen Dedorath, and Dirk Basenach   (1989)
  23. //
  24. // Implementation follows
  25. // Kurt Mehlhorn : "Data Structures and Algorithms 1", section III, 5.2 - 5.3
  26. //
  27. // Modifications:
  28. // -------------
  29. //
  30. // - member-function insert_at_item added  ( Michael Wenzel , Nov. 89)
  31. //
  32. // - virtual compare function ( Michael Wenzel , Nov. 89)
  33. //
  34. // - delete : overwrite copies of inner nodes by
  35. //            following links between different
  36. //            instances of the same key    ( Michael Wenzel , Jan. 90)
  37. //
  38. // - virtual clear functions  ( S. Naeher, Mai 90)
  39. //
  40. //------------------------------------------------------------------------------
  41.  
  42. #include    <LEDA/basic.h>
  43.  
  44. //------------------------------------------------------------------------------
  45. //  some declarations
  46. //------------------------------------------------------------------------------
  47.  
  48.  
  49. class ab_tree;
  50. class ab_tree_node;
  51.  
  52. typedef ab_tree_node* abnode;
  53.  
  54. typedef ab_tree_node* ab_tree_item;
  55.  
  56.  
  57. /*------------------------------------------------------------*/
  58. /*  class ab_tree_node                                        */
  59. /*------------------------------------------------------------*/
  60.  
  61. class ab_tree_node {
  62.   friend class ab_tree;
  63.  
  64.   friend void concat(ab_tree& s1,ab_tree& s2,ab_tree_node* current,GenPtr cur_key);
  65.   friend void pr_ab_tree(ab_tree_node* localroot,int blancs);
  66.   friend void del_ab_tree(ab_tree_node* localroot);
  67.  
  68.  
  69.   int p;           /* number of sons,a<=p<=b,0 iff leave*/
  70.   int height;      /* height of node*/
  71.   int index;       /* v is index'th son of his father*/
  72.   ab_tree_node* father;   
  73.   GenPtr* k;    /* array[1..b] --------------------------  */
  74.                          //-----------------------------------
  75.                          /*to every node v of T we assign p(v)-1 */
  76.                          /*elements k[1](v),...,k[p(v)-1](v) of U*/
  77.                          /* such that for all i (1<i<p(v)-1):*/
  78.                          /* k[i](v) < k[i+1](v) and for all leaves*/
  79.                          /* w in the i-th subtree of v we have*/
  80.                          /* k[i-1] < key[w] <= k[i](v) */
  81.                          /*------------------------------------*/
  82.                          /* if v is a leave ==> k[1]=key[v]*/
  83.  
  84.                          
  85.   ab_tree_node** son;    /* array [1..b+1] of pointer to sons*/
  86.   ab_tree_node** same;   /* m.w. : links between same keys */
  87.                          /* array [1..b]                   */
  88.   GenPtr inf;
  89.  
  90. /* size = 8 words  */
  91.  
  92. public:
  93.  
  94.   ab_tree_node(int p,int height,int index,ab_tree_node* father,int b);
  95.  
  96.  ~ab_tree_node();
  97.  
  98.   LEDA_MEMORY(ab_tree_node)
  99.  
  100.  }; 
  101.   
  102. /*-----------------------------------------------------------------*/
  103. class ab_tree   
  104.    {
  105.  
  106.     friend class ab_tree_node;
  107.  
  108.     friend void concat(ab_tree&,ab_tree&,ab_tree_node*,GenPtr); 
  109.  
  110.  
  111.     friend void pr_ab_tree(ab_tree_node* localroot,int blancs);
  112.     friend void del_ab_tree(ab_tree_node* localroot);
  113.  
  114.     int a;
  115.     int b;
  116.  
  117.     int height;             /* height of tree   */
  118.     int count;              /* number of leaves */
  119.  
  120.     ab_tree_node* root;
  121.     ab_tree_node* minimum;  
  122.     ab_tree_node* maximum;
  123.  
  124.     ab_tree_node* expand(ab_tree_node* v,int i);
  125.  
  126.     void split_node(ab_tree_node* v);
  127.     void share(ab_tree_node* v,ab_tree_node* y,int direct);
  128.     void fuse (ab_tree_node* v,ab_tree_node* w);
  129.     void del_tree(ab_tree_node* localroot);
  130.     void exchange_leaves(ab_tree_node*, ab_tree_node*);
  131.     void pr_ab_tree(ab_tree_node*,int) const;
  132.  
  133.  
  134.  
  135.     ab_tree_node* copy_ab_tree(ab_tree_node*,abnode&,int) const;
  136.  
  137.     virtual int cmp(GenPtr x, GenPtr y) const { return compare(x,y); }
  138.     virtual void clear_key(GenPtr&) const {}
  139.     virtual void clear_inf(GenPtr&) const {}
  140.     virtual void copy_key(GenPtr&)  const {}
  141.     virtual void copy_inf(GenPtr&)  const {}
  142.     virtual void print_key(GenPtr)  const {}
  143.     virtual void print_inf(GenPtr)  const {}
  144.  
  145. protected:
  146.  ab_tree_item item(void* p) const { return ab_tree_item(p); }
  147.  
  148.  
  149. public:
  150.  
  151.  
  152.  
  153.     void clear();
  154.  
  155.     GenPtr  inf(ab_tree_node* p)  const { return p->inf; }
  156.     GenPtr  key(ab_tree_node* p)  const { return p->k[1]; }
  157.  
  158.     ab_tree_node* insert(GenPtr, GenPtr);
  159.     ab_tree_node* insert_at_item(ab_tree_node*, GenPtr, GenPtr);
  160.     ab_tree_node* locate(GenPtr) const;
  161.     ab_tree_node* locate_succ(GenPtr) const;
  162.     ab_tree_node* locate_pred(GenPtr) const;
  163.     ab_tree_node* lookup(GenPtr) const;
  164.  
  165.     void del(GenPtr);
  166.     void del_item(ab_tree_node*);
  167.     void change_inf(ab_tree_node*, GenPtr);
  168.     void reverse_items(ab_tree_node*, ab_tree_node*);
  169.  
  170.     ab_tree& conc(ab_tree&);
  171.     void split_at_item(ab_tree_node* w,ab_tree& L,ab_tree& R);
  172.  
  173.     void del_min()                 { if (minimum) del_item(minimum); }
  174.     void decrease_key(ab_tree_node* p, GenPtr k) { GenPtr i = p->inf;
  175.                                                 copy_key(i);
  176.                                                 del_item(p);
  177.                                                 insert(k,i);
  178.                                                 clear_key(i);
  179.                                                }
  180.  
  181.  
  182.     bool member(GenPtr k)  const { return (lookup(k))? true: false ; }
  183.  
  184.     ab_tree_node* min()                      const { return minimum; }
  185.     ab_tree_node* find_min()                 const { return minimum; }
  186.     ab_tree_node* max()                      const { return maximum; }
  187.     ab_tree_node* first_item()               const { return minimum; }
  188.     ab_tree_node* next_item(ab_tree_node* p) const { return p->son[2]; }
  189.     ab_tree_node* succ(ab_tree_node* p)      const { return p->son[2]; }
  190.     ab_tree_node* pred(ab_tree_node* p)      const { return p->son[1]; }
  191.  
  192.     int  size()  const { return count;       }
  193.     bool empty() const { return (count==0) ? true : false;   }
  194.     void print() const { pr_ab_tree(root,1); }
  195.  
  196.     ab_tree(int a_=2,int b_=4); 
  197.     ab_tree(const ab_tree& T);
  198.  
  199.     ab_tree& operator=(const ab_tree&);
  200.  
  201.     virtual ~ab_tree(){ clear(); }
  202.  };
  203.  
  204. #endif
  205.